package com.malou.client.reportes;

import java.util.Date;
import java.util.HashMap;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.RootPanel;
import com.malou.client.MalouConstants;

public class GReporteSaldosPanel extends ContentPanel {
	DateField desde;
	DateField hasta;
	Button find;
	Button print;
	Button excel;
	Button mail;
	Status status;
	Status status_xls;	
	ContentPanel hidePanel;
	EnviarCorreoDialog sendCorreo;
	private MalouConstants constants = GWT.create(MalouConstants.class);
	
	public GReporteSaldosPanel(){
		setLayout(new FitLayout());
		setFrame(false);		
		setIcon(IconHelper.createPath("images/icons/table.png"));  
        setHeading(constants.reportesaldos());
		
        desde = new DateField();        
        desde.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
        desde.setAllowBlank(false);
        desde.setMaxValue(new Date());
        
        hasta = new DateField();       
        	
        hasta.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
        hasta.setAllowBlank(false);
        hasta.setValue(new Date());
        hasta.setMaxValue(new Date());
        
        hasta.setValidator(new Validator() {			
			@Override
			public String validate(Field<?> field, String value) {
				if(desde.getValue() !=null && hasta.getValue() != null){
					if(desde.getValue().compareTo(hasta.getValue())>0)
						return constants.debesermayor();
					return null;
				}
				return null;
			}
		});
        final GReporteSaldosPanel rpv = this;        
        
        ToolBar toolBar = new ToolBar();
        
        toolBar.add(new Label(constants.desde()));
        toolBar.add(desde);
                
        Label hastaL = new Label(constants.hasta());
        hastaL.setStyleAttribute("paddingLeft", "20px");
        toolBar.add(hastaL);
        toolBar.add(hasta);
        
        status = new Status();
	    status.setBusy(constants.cargando());
	    status.hide();    
	    status.setAutoWidth(true);
	    status.setStyleAttribute("paddingLeft", "20px");
	    toolBar.add(status);
        
        find = new Button(constants.find(), IconHelper.createPath("images/icons/find.png"));
        find.setStyleAttribute("paddingLeft", "20px");
        
        SelectionListener<ButtonEvent> sl_find;        
        sl_find = new SelectionListener<ButtonEvent>(){
        	
        	@Override
			public void componentSelected(ButtonEvent ce) {		
				if(validate()){
					status.show();
					find.hide();
					rpv.removeAll();
					HashMap<String, Object> param = new HashMap<String, Object>();
					param.put("desde", desde.getValue());
					param.put("hasta", hasta.getValue());				
					PdfReportViewer reportViewer = new PdfReportViewer("reports/g_reporte_resumen", param, status,find,print, 4);
					
					rpv.add(reportViewer);
					rpv.layout();
				}
			}
        };
        find.addSelectionListener(sl_find);
        
        toolBar.add(find);
        
        print = new Button(constants.imprimir(), IconHelper.createPath("images/icons/print.png"));
        print.setStyleAttribute("paddingLeft", "20px");
        print.setEnabled(false);
                
        toolBar.add(print);
        
        status_xls = new Status();
        status_xls.setBusy(constants.cargando());
        status_xls.hide();    
        status_xls.setAutoWidth(true);
        status_xls.setStyleAttribute("paddingLeft", "20px");
        toolBar.add(status_xls);
        
        excel = new Button(constants.exportToXls(), IconHelper.createPath("images/icons/excel.png"));
        excel.setStyleAttribute("paddingLeft", "20px");        
        
        SelectionListener<ButtonEvent> sl_excel;        
        sl_excel = new SelectionListener<ButtonEvent>(){
        	
        	@Override
			public void componentSelected(ButtonEvent ce) {		
				if(validate()){
					status_xls.show();
					excel.hide();
					hidePanel.removeAll();
					
					HashMap<String, Object> param = new HashMap<String, Object>();
					param.put("desde", desde.getValue());
					param.put("hasta", hasta.getValue());				
					XlsReportDownload reportViewer = new XlsReportDownload("reports/g_reporte_resumen", param, status_xls, excel, 4);
										
					hidePanel.add(reportViewer);
					hidePanel.layout();
				}
			}
        };
        excel.addSelectionListener(sl_excel);
        
        toolBar.add(excel);
        
        mail = new Button(constants.enviarCorreo(), IconHelper.createPath("images/icons/mail.png"));
        mail.setStyleAttribute("paddingLeft", "20px");        
        
        SelectionListener<ButtonEvent> sl_mail;
        sendCorreo = new EnviarCorreoDialog();
        sl_mail = new SelectionListener<ButtonEvent>(){
        	
        	@Override
			public void componentSelected(ButtonEvent ce) {		
				if(validate()){
					sendCorreo.reset();
					sendCorreo.rptGSaldos = GReporteSaldosPanel.this;
					sendCorreo.show();					
				}
			}
        };
        mail.addSelectionListener(sl_mail);
        
        toolBar.add(mail);
        
        setTopComponent(toolBar);
        
        if(hidePanel!= null)
        hidePanel.removeFromParent();
        hidePanel= new ContentPanel();        
        hidePanel.setVisible(false);        
        RootPanel.get().add(hidePanel);        
        
	}
	private boolean validate() {
		boolean des = desde.isValid();
		boolean has = hasta.isValid();
		
		return des & has;
	}
	
	public void enviarEmail(EnviarCorreo send){
		send.setSubject(constants.reportesaldos());
		
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("desde", desde.getValue());
		param.put("hasta", hasta.getValue());
		
		Reportes.correoDialog = sendCorreo;
		Reportes.enviarEmail("reports/g_reporte_resumen", param, send, 4);
	}
}
